<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
	String wsBasePath = "ws://"+request.getServerName()+":"+request.getServerPort()+path+"/";
	session.setAttribute("farmUsername", "userA");
%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <link rel="stylesheet" type="text/css" href="<%=basePath%>ext/easyui/themes/black/easyui.css?t=34355">
    <link rel="stylesheet" type="text/css" href="<%=basePath%>ext/easyui/themes/icon.css">
    <link rel="stylesheet" type="text/css" href="<%=basePath%>ext/easyui/themes/color.css">
    <link rel="stylesheet" type="text/css" href="<%=basePath%>ext/farm/farm.css">
    <link rel="stylesheet" type="text/css" href="<%=basePath%>ext/seed/grid.css">
    <link rel="stylesheet" type="text/css" href="<%=basePath%>ext/user/userChoice.css">
    <script type="text/javascript" src="<%=basePath%>ext/farm/utils.js"></script>
    <script type="text/javascript" src="<%=basePath%>ext/easyui/jquery.min.js"></script>
    <script type="text/javascript" src="<%=basePath%>ext/easyui/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="<%=basePath%>ext/easyui/plugins/jquery.edatagrid.js"></script>
    <script type="text/javascript" src="<%=basePath%>ext/easyui/locale/easyui-lang-zh_CN.js"></script>
    <script type="text/javascript" src="<%=basePath%>ext/farm/sockjs.js"></script>  
</head>
<style type="text/css">
	body, html{
		width: 100%; 
		height: 100%; 
		background-image: url(<%=basePath%>/images/backgroundImg.png);
	}
</style>
<body style="width: 100%; height: 100%;">
<div id="userChoiceDialog" class="easyui-window" title="用户选择" minimizable="false" collapsible="false" maximizable="false"
	 closable="false" data-options="iconCls:'icon-user', closed: false" 
	 style="width: 400px; height: 150px; padding: 4px;" scrolling="no">
	<form id="userChoiceInput" style="margin-top: 12px" class="fileInput" method="POST" novalidate>
		当前用户：<input style="width: 300px" id="userInfo" class="easyui-combobox" panelHeight="auto"
			data-options="valueField: 'id',
						  textField: 'userName',
                          required: true,
                          method: 'get',
                          url: '<%=basePath%>/user/list',
                          formatter: formatItem
                         "/>
		<div class="userChoiceButtons">
			<a style="font-size: 10px">请在下拉框中选择用户信息，并点击“确定”按钮设定当前用户信息。</a>
			<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" 
				onclick="showUserInfo()">确定</a>
		</div>
	</form>
</div>

<script type="text/javascript">

	/* WebSocket */
	var websocket = null; 
	function initWebSocket(){	    	     
	    if ('WebSocket' in window) {  
	        //Websocket的连接  
	        websocket = new WebSocket("<%=wsBasePath%>/farm/action");//WebSocket对应的地址  
	    }  
	    else if ('MozWebSocket' in window) {  
	        //Websocket的连接  
	        websocket = new MozWebSocket("<%=wsBasePath%>/farm/action");//SockJS对应的地址  
	    }  
	    else {  
	        //SockJS的连接  
	        websocket = new SockJS("<%=wsBasePath%>/farm/action");//SockJS对应的地址    
	    }  
	    websocket.onopen = onOpen;  
	    websocket.onmessage = onMessage;  
	    websocket.onerror = onError;  
	    websocket.onclose = onClose;
	}

	function onOpen(evt) {  
	    console.log("连接打开：",evt);  
	}  

	function onMessage(evt) {
		console.log("信息 === ");
		console.log(evt);
	}  
	function onError(evt) {  
		console.log("出现错误：",evt);
	}  
	function onClose(evt) {  
		console.log("连接关闭：",evt);
	} 
	
	//点击种子商店后，恢复页面原布局
	parent.document.getElementById("tools").src = "tools.jsp";
	/* window.parent.document.body.rows='60,*,50'; */

	var valueDate = 0;
	// 左上角用户信息
	function formatItem(row) {
		return '<img style="float: left;" height="25px" src="<%=basePath%>/images/headImages/' + row.headImg + '">' +
			'<a style="margin-left: 5px; float: left; font-size: 10px; display: block; height: 25px; line-height: 25px">' + row.userName + " | 经验：" + row.experience + " | 金币：" + 
			row.money + " | 积分：" + row.point + '</a><br>';
	}
	
	function showUserInfo(){
		// 获取当前选中行的对象
		var id = $("#userInfo").combobox("getValue");
		var user = {};
		user.id = id;
		var url = "<%=basePath%>/user/setCurUser";
		request(user, "post", url, showMessage);
		// 登录后打开websocket
		initWebSocket();
    }
	
	function showMessage(result){
		parent.document.getElementById("menu").src = "menu.jsp";
		$.messager.show({
            title: "消息",
            msg: result.msg
        });
	}
	
    /* window.close = function () {  
        websocket.onclose();  
    } */
  
</script>
</body>
</html>